package com.yzt.minemodule.fragment;

import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.yzt.zhuju.RXingUtils;
import com.yzt.zhuju.arouter.RouterMessage;
import com.yzt.zhuju.arouter.RouterUser;
import com.yzt.zhuju.base.BaseFragment;
import com.yzt.zhuju.network.AresSubscriber;
import com.yzt.zhuju.network.biz.BizLayer;
import com.yzt.zhuju.network.entity.mine.FriendBaseBean;
import com.yzt.zhuju.network.entity.mine.FriendUserBean;
import com.yzt.zhuju.network.entity.user.FollowUserStatusRequest;
import com.yzt.zhuju.utils.ViewUtil;
import com.yzt.zhuju.widget.refreshHeader.WaterDropHeader;
import com.yzt.zhuju.widget.tuiview.TUIEditText;
import com.yzt.minemodule.R;
import com.yzt.minemodule.adapter.FriendListAdapter;
import com.yzt.plugin_network.response.HttpResponse;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

/**
 * author : bob
 * date   : 2022/12/12
 * desc   : 好友列表子页面
 */
public class FriendListFragment extends BaseFragment implements OnRefreshListener {

    /**
     * 好友的类型
     */
    private int type;

    private String mUserId;

    private FriendListAdapter friendListAdapter;

    private RefreshLayout mRefreshLayout;

    private TUIEditText etContent;

    private TextView tvCancel;

    private String keyword = "";

    public static FriendListFragment newInstance(int type, String mUserId) {
        FriendListFragment fragment = new FriendListFragment();
        fragment.type = type;
        fragment.mUserId = mUserId;
        return fragment;
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_friend_list;
    }

    @Override
    public void installViews() {

        etContent = findViewById(R.id.et_content);
        tvCancel = findViewById(R.id.tv_cancel);

        tvCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tvCancel.setVisibility(View.GONE);
                etContent.setText("");
            }
        });

        etContent.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                String content = etContent.getText().toString().trim();
                if (content.equals("")) {
                    tvCancel.setVisibility(View.GONE);
                } else {
                    tvCancel.setVisibility(View.VISIBLE);
                }
                keyword = content;
                mCurrentPage = 1;
                getHttpData();
            }
        });

        // 初始化列表
        mRefreshLayout = findViewById(R.id.refreshLayout);
        mRefreshLayout.setRefreshHeader(new WaterDropHeader(mActivity));
        mRefreshLayout.setOnRefreshListener(this);

        RecyclerView mListRv = findViewById(R.id.list_rv);
        mListRv.setLayoutManager(new LinearLayoutManager(mActivity));
        friendListAdapter = new FriendListAdapter();
        mListRv.setAdapter(friendListAdapter);
        friendListAdapter.setType(type);
        friendListAdapter.showMore(mUserId.equals(RXingUtils.getPersonDetail().getBasic().getUserId()));

        friendListAdapter.setOnItemClickListener((adapter, view, position) -> {
            if (ViewUtil.isFastDoubleClick()) {
                return;
            }
            FriendUserBean userInfo = (FriendUserBean) adapter.getData().get(position);
            RouterUser.PersonalDetailActivity(userInfo.getUserId());
        });

        friendListAdapter.addChildClickViewIds(R.id.follow_tv, R.id.avater_riv);
        friendListAdapter.setOnItemChildClickListener((adapter, view, position) -> {
            if (ViewUtil.isFastDoubleClick()) {
                return;
            }
            FriendUserBean user = (FriendUserBean) adapter.getData().get(position);
            if (view.getId() == R.id.follow_tv) {
                if (type == RouterUser.FriendType.TYPE_FRIEND) {
                    RouterMessage.ConversationActivity(user.getUserId(), user.getNickName(), false);
                } else {
                    FollowUserStatusRequest mFollowRequest = new FollowUserStatusRequest();
                    List<String> list = new ArrayList<>();
                    list.add(user.getUserId());
                    mFollowRequest.setUidRels(list);
                    if (type == RouterUser.FriendType.TYPE_FOLLOW) {
                        mFollowRequest.setStatus(0);
                    } else {
                        mFollowRequest.setStatus(1);
                    }
                    BizLayer.getInstance().getAccountModule().postFollowOrUnfollowUser(mFollowRequest, new AresSubscriber<HttpResponse>() {
                        @Override
                        protected void onSuccess(HttpResponse httpResponse) {
                            mRefreshLayout.autoRefresh();
                        }
                    });
                }
            }
        });

        friendListAdapter.getLoadMoreModule().setOnLoadMoreListener(() -> {
            mCurrentPage++;
            getHttpData();
        });

        mRefreshLayout.autoRefresh();
    }

    @Override
    public void netWorkRequest() {

    }

    public void getHttpData() {
        BizLayer.getInstance().getAccountModule().friendList(type, keyword, mUserId, mCurrentPage, PAGE_SIZE, new AresSubscriber<FriendBaseBean>() {
            @Override
            protected void onSuccess(FriendBaseBean list) {
                if (mCurrentPage == 1) {
                    if (list.getList().size() == 0) {
                        friendListAdapter.setEmptyView(getEmptyView(R.mipmap.icon_empty_friend, getString(R.string.empty_friend)));
                    } else {
                        friendListAdapter.removeEmptyView();
                    }
                    friendListAdapter.setList(list.getList());
                } else {
                    friendListAdapter.addData(list.getList());
                }
                friendListAdapter.getLoadMoreModule().loadMoreComplete();
                if (list.getList().size() < PAGE_SIZE) {
                    friendListAdapter.getLoadMoreModule().loadMoreEnd();
                }
                hideLayoutPage();
            }

            @Override
            protected void onError(int code, String info) {
                super.onError(code, info);
                if (mCurrentPage == 1) {
                    isNetWorking = false;
                    friendListAdapter.setEmptyView(getErrorView(info));
                }
            }

            @Override
            protected void onFinallyEnd() {
                super.onFinallyEnd();
                mRefreshLayout.finishRefresh();
                hideLayoutPage();
            }
        });
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        mCurrentPage = 1;
        getHttpData();
    }
}

